////////////////////////////////////////////////////旅游足迹////////////////////////////////////
const config = require('../config');

module.exports = function (app, query, jwt) {
  /* if (authority === 1) { } else {
    const json = {
      code: false,
      message: "您的用户没有权限做相关操作"
    };
    console.log(json);
    res.json(json);
  } */
 
  //根据用户列表
  app.get('/backstage/userList',jwt({ secret: config.secret }), function (req, res, next) {
    console.log(req.user);
    console.log(req);
    const { authority } = req.user;
    let { pageSize, current } = req.query;
      current = Number(current);
      pageSize = Number(pageSize);
    if (authority === 1) {
      if (pageSize === NaN) {
        pageSize = 10
      }
      if (current === NaN) {
        current = 1
      }
      let sql = `select * from user`;
      sql += ` limit ${pageSize * (current - 1)},${pageSize};SELECT FOUND_ROWS();`;

      query(sql, function (err, rows, fields) {
        if (err) {
          const json = {
            code: false,
            message: err.message
          };
          console.log(json);
          res.json(json);
        } else {
          console.log(rows)
          if (rows[1] && rows[1].length > 0) {
            let list = Array.from(rows[0]).map(item => {
              return {
                userId: item.userId,
                username: item.username,
                authority: item.authority
              }
            });
            const data = {
              current,
              pageSize,
              total: rows[1][0]['FOUND_ROWS()'],
              list
            }
            const json = {
              code: true,
              message: '查询成功',
              data
            };
            console.log(json);
            res.json(json);
          }
        }
      });
    } else {
      const json = {
        code: false,
        message: "您的用户没有权限做相关操作"
      };
      console.log(json);
      res.json(json);
    }
  });

  //更改权限或者用户名
  app.put('/backstage/user', jwt({ secret: config.secret }), function (req, res, next) {
    console.log(req.user);
    console.log(req.body);
    const { authority } = req.user;
    const { userid, username, role } = req.body;
    if (authority === 1) {
      query('SELECT * FROM  user WHERE userid=?', [userid], function (err, rows, fields) {
        if (err) {
          const json = {
            code: false,
            message: err.message
          };
          console.log(json);
          res.json(json);
        } else {
          if (rows && rows.length > 0) {
            query('update user set  password=? where username=?', [username, role], function (err, data, field) {
              if (err) {
                const json = {
                  code: false,
                  message: err.message
                };
                console.log(json);
                res.json(json);
              } else {
                const json = {
                  code: true,
                  message: '该用户更新成功'
                };
                console.log(json);
                res.json(json);
              }
            })
          }
        }
      })
    } else {
      const json = {
        code: false,
        message: "您的用户没有权限做相关操作"
      };
      console.log(json);
      res.json(json);
    }
  });

  //新建用户
  app.post('/backstage/user', function (req, res, next) {
    const { authority } = req.user;
    const { username, role } = req.body;
    if (authority === 1) {
      query('SELECT * FROM user WHERE username=?', [username], function (err, rows, field) {
        if (err) {
          const json = {
            code: false,
            message: err.message
          };
          console.log(json);
          res.json(json);
        } else {
          if (rows && rows.length > 0) {
            const json = {
              code: false,
              message: "用户名已存在，新建失败"
            };
            console.log(json);
            res.json(json);
          } else {
            const user = {
              username,
              authority: role,
              password: md5("123456")
            };
            query('insert into user set ?', user, function (error, result) {
              if (err) {
                const json = {
                  code: false,
                  message: err.message
                };
                console.log(json);
                res.json(json);
              } else {
                const json = {
                  code: true,
                  message: "用户新建成功"
                };
                console.log(json);
                res.json(json);
              }
            });
          }
        }
      });
    } else {
      const json = {
        code: false,
        message: "您的用户没有权限做相关操作"
      };
      console.log(json);
      res.json(json);
    }
  });

  //删除用户
  app.delete('/backstage/user', jwt({ secret: config.secret }), function (req, res, next) {
    const { authority } = req.user;
    const { userid } = req.query;
    if (authority === 1) {
      query('delete from user where userid=?', [userid], function (error, data, field) {
        if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
        } else {
          const json = {
            code: true,
            message: "删除成功"
          };
          console.log(json);
          res.json(json);
        }
      })
    } else {
      const json = {
        code: false,
        message: "您的用户没有权限做相关操作"
      };
      console.log(json);
      res.json(json);
    }
  });
  
  
  
  //展示攻略列表
  app.get('/backstage/guidelist', function (req, res, next) {  
    let { pageSize, current } = req.query;  
	current = Number(current);
    pageSize = Number(pageSize); 
    if (pageSize === NaN) {
      pageSize = 10
    }
    if (current === NaN) {
      current = 1
    }
     query(` select  * from article where state=0 limit ${pageSize * (current - 1)},${pageSize} ;SELECT FOUND_ROWS();`, function (err, rows, field) {
        if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
        if (rows[1] && rows[1].length > 0) {
          let list = Array.from(rows[0]);
          const data = {
            current,
            pageSize,
            total: rows[1][0]['FOUND_ROWS()'],
            list
          }
		     const json = {
            code: true,
            message: '查询成功',
            data
          };
        console.log(json);
        res.json(json);
      }
	  }
    });
  });



  //删除攻略
  app.delete('/backstage/guide', jwt({ secret: config.secret }),function (req, res, next) {
    const { authority } = req.user;
    const { userid } = req.query;
    if (authority === 1) {
		    query(`delete from article where id=?`,[req.body.guideid], function (err, data, field) {
         if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
        res.json(data);
      }
    });
		} else {
      const json = {
        code: false,
        message: "您的用户没有权限做相关操作"
      };
      console.log(json);
      res.json(json);
    }

  });

  //新建攻略
  app.post('/backstage/guide', jwt({ secret: config.secret }), function (req, res, next) {
       const { authority } = req.user;
    const { userid } = req.query;
    var guide={title:req.body.guidetitle,author_name:req.body.authorname,class_tag:req.body.guidesrc,content:req.body.guidetext};
    if (authority === 1) {
		 query(` insert into article set ? `,guide , function (err, data, field) {
        if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
        res.json(data);
      }
    });
		} else {
      const json = {
        code: false,
        message: "您的用户没有权限做相关操作"
      };
      console.log(json);
      res.json(json);
    }   
  
  });


  //修改攻略
  app.put('/backstage/guide', jwt({ secret: config.secret }), function (req, res, next) {
        const { authority } = req.user;
    const { userid } = req.query;
    var guide={title:req.body.guidetitle,author_name:req.body.authorname,class_tag:req.body.guidesrc,content:req.body.guidetext};
        if (authority === 1) {
				query(`update article   set  ?  where id=?  `,[guide,req.body.guideid] , function (err, data, field) {
        if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
        console.log(req.body.guideid);
        res.json(data);
      }
    });
		} else {
      const json = {
        code: false,
        message: "您的用户没有权限做相关操作"
      };
      console.log(json);
      res.json(json);
    } 
  });

   //发表旅游游记
  app.post('/backstage/note', jwt({ secret: config.secret }), function (req, res, next) {
        const { authority } = req.user;
    const { userid } = req.query;
    const {title, author, date, text} = req.body;
    const report = {
      author_name:author,
      date,
      itle: title,
      state: 1,
      content: text
    };
	    if (authority === 1) {
			   query('insert into article set ?', report, function (err, rows, field) {
        if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
        console.log('发表游记成功');
        return;
      }
    });
		} else {
      const json = {
        code: false,
        message: "您的用户没有权限做相关操作"
      };
      console.log(json);
      res.json(json);
    }
 
  });
 
 
  //展示游记列表
  app.get('/backstage/notelist', function (req, res, next) { 
     let { pageSize, current } = req.query;  
	current = Number(current);
    pageSize = Number(pageSize); 
    if (pageSize === NaN) {
      pageSize = 10
    }
    if (current === NaN) {
      current = 1
    }
	 query(` select  * from article where state in (1,2) limit ${pageSize * (current - 1)},${pageSize} ;SELECT FOUND_ROWS();`, function (err, rows, field) {
          if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
        if (rows[1] && rows[1].length > 0) {
          let list = Array.from(rows[0]);
          const data = {
            current,
            pageSize,
            total: rows[1][0]['FOUND_ROWS()'],
            list
          }
		     const json = {
            code: true,
            message: '查询成功',
            data
          };
        console.log(json);
        res.json(json);
      }
	  }
    }); 
  });


  //修改游记
  app.put('/backstage/note', jwt({ secret: config.secret }), function (req, res, next) {
        const { authority } = req.user;
    const { userid } = req.query; 
        if (authority === 1) {
  query(`update article   set  title=?,author_name=?,state=?,content=? where id=?  `,[req.body.notetitle,req.body.authorname,req.body.state,req.body.notetext,req.body.noteid] , function (err, data, field) {
        if (err) {
        const json = {
          code: false,
          message: err.message
        };  
        console.log(json);
	  res.json(json);
      } else { 
	  const json = {
            code: true,
            message: '修改成功', 
          };
        console.log(json);
        res.json(data);
      }
    });
		} else {
      const json = {
        code: false,
        message: "您的用户没有权限做相关操作"
      };
      console.log(json);
      res.json(json);
    } 
  });

  //删除游记
  app.delete('/backstage/note', jwt({ secret: config.secret }), function (req, res, next) {
        const { authority } = req.user;
    const { userid } = req.query;
	    if (authority === 1) {
			    query(`delete from article where  id=?`,[req.body.noteid], function (err, data, field) {
         if (err) {
        const json = {
          code: false,
          message: err.message
        };
        console.log(json);
        res.json(json);
      } else {
        res.json(data);
      }
    });
		} else { 
      const json = {
        code: false,
        message: "您的用户没有权限做相关操作"
      };
      console.log(json);
      res.json(json);
    }

  });
 
  
  
};

